﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Csla;

namespace Model
{
    [Serializable()]
    public class ThietDatBOCollection : Csla.BusinessListBase<ThietDatBOCollection, ThietDatBO>
    {
        #region BindingList Overrides
        protected override object AddNewCore()
        {
            ThietDatBO _ThietDatBO = ThietDatBO.NewChildThietDatBO();
            OnAddingNew(new System.ComponentModel.AddingNewEventArgs(_ThietDatBO));
            Add(_ThietDatBO);
            return _ThietDatBO;
        }
        #endregion BindingList Overrides
        //==========================================================================

        #region Factory Methods
        private ThietDatBOCollection()
        {
        }

        public static ThietDatBOCollection NewThietDatBOCollection()
        {
            return new ThietDatBOCollection();
        }

        public static ThietDatBO NewThietDatBO()
        {
            return ThietDatBO.NewChildThietDatBO();
        }

        public static ThietDatBOCollection GetThietDatBOCollection()
        {
            SPParam spParam = new SPParam("ThietDatID", "ASC", "WHERE DELE=0");
            return DataPortal.Fetch<ThietDatBOCollection>(spParam);
        }

        public static ThietDatBOCollection GetThietDatBOCollection(bool getDeleted)
        {
            SPParam spParam = new SPParam("ThietDatID", "ASC", "");
            return DataPortal.Fetch<ThietDatBOCollection>(spParam);
        }

        public static ThietDatBOCollection GetThietDatBOCollection(string where)
        {
            SPParam spParam = new SPParam("ThietDatID", "ASC", "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<ThietDatBOCollection>(spParam);
        }

        public static ThietDatBOCollection GetThietDatBOCollection(string where, bool getDeleted)
        {
            SPParam spParam = new SPParam("ThietDatID", "ASC", (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<ThietDatBOCollection>(spParam);
        }

        public static ThietDatBOCollection GetThietDatBOCollection(string where, string orderByColumn)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "ThietDatID" : orderByColumn), "ASC", "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<ThietDatBOCollection>(spParam);
        }

        public static ThietDatBOCollection GetThietDatBOCollection(string where, string orderByColumn, bool getDeleted)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "ThietDatID" : orderByColumn), "ASC", (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<ThietDatBOCollection>(spParam);
        }

        public static ThietDatBOCollection GetThietDatBOCollection(string where, string orderByColumn, string orderByExpression)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "ThietDatID" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<ThietDatBOCollection>(spParam);
        }

        public static ThietDatBOCollection GetThietDatBOCollection(string where, string orderByColumn, string orderByExpression, bool getDeleted)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "ThietDatID" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<ThietDatBOCollection>(spParam);
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        private class Criteria
        {
            public Criteria()
            {
            }
        }

        [Serializable()]
        public class FilterCriteria
        {
            public FilterCriteria()
            {
            }
        }
        #endregion Criteria
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Fetch(SPParam spParam)
        {
            RaiseListChangedEvents = false;
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("ThietDatBO_SelectBOCollection");
            db.AddInParameter(cmd, "@OrderColumn", SqlDbType.VarChar, (object)spParam.orderColumn);
            db.AddInParameter(cmd, "@OrderExpression", SqlDbType.VarChar, (object)spParam.orderExpression);
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, (object)spParam.whereExpression);
            using (SqlDataReader dr = (SqlDataReader)db.ExecuteReader(cmd))
            {
                while (dr.Read())
                {
                    ThietDatBO objThietDatBO = ThietDatBO.GetChildThietDatBO(dr);
                    this.Add(objThietDatBO);
                }
            }
            RaiseListChangedEvents = true;
        }
        #endregion Fetch
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            Update();
        }

        internal void Update()
        {
            LogBOCollection logBOCollection = LogBOCollection.NewLogBOCollection();

            RaiseListChangedEvents = false;
            ThietDatBO child = null;
            int i = 0;
            int length = this.Count;
            bool del = false;
            for (i = 0; i < length; i++)
            {
                child = this[i];
                del = StaticUtil.GetBool(child.DELE);

                if (child.IsNew && !del)
                {
                    child.Insert();
                    logBOCollection.Add(StaticUtil.CreateLog(child, "ThietDatID", TacVu.Them, "ThietDat", child.ThietDatID.Value));
                }
                else
                {
                    child.Update();
                    logBOCollection.Add(StaticUtil.CreateLog(child, "ThietDatID", (child.DELE.Value ? TacVu.Xoa : TacVu.Sua), "ThietDat", child.ThietDatID.Value));
                }

                if (del)
                {
                    this.RemoveAt(i);
                    i--;
                    length--;
                }
            }
            RaiseListChangedEvents = true;

            //-----
            logBOCollection = logBOCollection.Save();
        }
        #endregion Update
        //==========================================================================

        #region Business Method
        public ThietDatBO GetThietDatBO(int _ThietDatID)
        {
            int length = this.Count;
            for (int i = 0; i < length; i++)
                if (this[i].ThietDatID == _ThietDatID)
                    return this[i];

            return null;
        }

        public ThietDatBO GetThietDatBO(LoaiThietDat loaiThietDat)
        {
            string sLoaiThietDat = "";
            switch (loaiThietDat)
            {
                case LoaiThietDat.DiaChi:
                    sLoaiThietDat = "DiaChi";
                    break;
                case LoaiThietDat.DienThoai:
                    sLoaiThietDat = "DienThoai";
                    break;
                case LoaiThietDat.Fax:
                    sLoaiThietDat = "Fax";
                    break;
                case LoaiThietDat.Website:
                    sLoaiThietDat = "Website";
                    break;
                case LoaiThietDat.Email:
                    sLoaiThietDat = "Email";
                    break;
                case LoaiThietDat.MaSoThue:
                    sLoaiThietDat = "MaSoThue";
                    break;
                case LoaiThietDat.GiayPhepKinhDoanh:
                    sLoaiThietDat = "GiayPhepKinhDoanh";
                    break;
                case LoaiThietDat.SoTaiKhoan:
                    sLoaiThietDat = "SoTaiKhoan";
                    break;
                case LoaiThietDat.NganHang:
                    sLoaiThietDat = "NganHang";
                    break;
                case LoaiThietDat.LuongToiThieu:
                    sLoaiThietDat = "LuongToiThieu";
                    break;
                case LoaiThietDat.Anh:
                    sLoaiThietDat = "Anh";
                    break;
                case LoaiThietDat.BHYT:
                    sLoaiThietDat = "BHYT";
                    break;
                case LoaiThietDat.BHXH:
                    sLoaiThietDat = "BHXH";
                    break;
                case LoaiThietDat.BHTN:
                    sLoaiThietDat = "BHTN";
                    break;
                case LoaiThietDat.NgayTinhLuong:
                    sLoaiThietDat = "NgayTinhLuong";
                    break;
                case LoaiThietDat.MatKhau:
                    sLoaiThietDat = "MatKhau";
                    break;
                case LoaiThietDat.GiamDoc:
                    sLoaiThietDat = "GiamDoc";
                    break;
                case LoaiThietDat.KeToan:
                    sLoaiThietDat = "KeToan";
                    break;
                case LoaiThietDat.ThuQuy:
                    sLoaiThietDat = "ThuQuy";
                    break;
                case LoaiThietDat.ThuKho:
                    sLoaiThietDat = "ThuKho";
                    break;
                case LoaiThietDat.PhienBan:
                    sLoaiThietDat = "PhienBan";
                    break;
            }

            int length = this.Count;
            for (int i = 0; i < length; i++)
                if (this[i].Loai == sLoaiThietDat)
                    return this[i];

            return null;
        }

        public string GetStrValue(LoaiThietDat loaiThietDat)
        {
            ThietDatBO thietDatBO = GetThietDatBO(loaiThietDat);
            if (thietDatBO != null)
                return thietDatBO.NoiDungS;
            else
                return null;
        }

        public double? GetDoubleValue(LoaiThietDat loaiThietDat)
        {
            ThietDatBO thietDatBO = GetThietDatBO(loaiThietDat);
            if (thietDatBO != null)
                return thietDatBO.NoiDungF;
            else
                return null;
        }

        public void SetThietDatBO(LoaiThietDat loaiThietDat, string value)
        {
            ThietDatBO thietDatBO = GetThietDatBO(loaiThietDat);
            if (thietDatBO != null)
                thietDatBO.NoiDungS = value;
        }

        public void SetThietDatBO(LoaiThietDat loaiThietDat, double value)
        {
            ThietDatBO thietDatBO = GetThietDatBO(loaiThietDat);
            if (thietDatBO != null)
                thietDatBO.NoiDungF = value;
        }

        public void SetThietDatBO(LoaiThietDat loaiThietDat, int? value)
        {
            ThietDatBO thietDatBO = GetThietDatBO(loaiThietDat);
            if (thietDatBO != null)
                thietDatBO.NoiDungF = value;
        }

        public void SetThietDatBO(LoaiThietDat loaiThietDat, object value)
        {
            ThietDatBO thietDatBO = GetThietDatBO(loaiThietDat);
            if (thietDatBO != null)
            {
                if (value == null)
                    thietDatBO.NoiDungI = null;
                else
                    thietDatBO.NoiDungI = (byte[])value;
            }
        }

        public void Remove(int ThietDatID)
        {
            int length = this.Count;
            for (int i = 0; i < length; i++)
            {
                if (this[i].ThietDatID == ThietDatID)
                {
                    try
                    {
                        this.RemoveAt(i);
                    }
                    catch { }
                    break;
                }
            }
        }
        #endregion Business Method
        //==========================================================================
    }
}
